<?php

namespace app\admin\controller;

use think\Db;

class Balance extends Common{

	/*
	资金记录
	 */
	public function index(){
		// $type = $data = Db::table('tp_balance_record_type')->where('status',1)->group('group')->field('group,group_name')->select();
		$type = Db::table('tp_balance_record_type')->where('status',1)->where('remark IS NOT NULL')->field('type,remark')->select();
		$wallet = Db::table('tp_coin_wallet')->where('status',1)->select();
		$this->assign('type',$type);
		$this->assign('wallet',$wallet);
		return $this->fetch();
	}
	
	public function dataList(){
		$limit      = request()->param('limit')?:20;
		$username   = request()->param('username');
		$phone   = request()->param('phone');
		$type   = request()->param('type');
		$remark   = request()->param('remark');
		$wallet_name  = request()->param('wallet_name');
		$uid   = request()->param('uid');
		$wid   = request()->param('wid');
		$page      = request()->param('page');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if ($remark != '') {
            $where .= ' and b.remark like \'%' . $remark . '%\'';
        }
        if ($phone != '') {
            $where .= ' and u.phone=' . $phone;
        }
        if($wallet_name != '') {
            $where .= ' and w.name like \'%'.$wallet_name . '%\'';
        }
        if($type != '') {
            $where .= ' and b.type='.$type;
        }
        // if($type != '') {
        //     $where .= ' and b.type>='.$type.' and b.type<'.($type+10);
        // }
        if($uid != '') {
            $where .= ' and b.uid='.$uid;
        }
        if($uid != '') {
			if (!in_array($uid, $this->data_uids_arr)) {
				return $this->tableData([]);
			}
			$where .= ' and b.uid='.$uid;
		} else {
			if ($this->data_uids_string) {
				$where .= ' and b.uid in ('.$this->data_uids_string.')';
			}
		}
        if($wid != '') {
            $where .= ' and b.wid='.$wid;
        }
        if($start_time != '') {
            $where .= ' and b.date>="'.strtotime($start_time).'"';
        }
		if($end_time != '') {
            $where .= ' and b.date<="'.strtotime($end_time).'"';
        }
		$data  = Db::table('tp_balance_record')
				->alias('b')
				->join('tp_user u','u.id = b.uid','LEFT')
				->join('tp_coin_wallet w','w.id = b.wid','LEFT')
				->where('b.date','>=',1621094400)
				->where($where)
				->field('b.*,u.username,u.phone,w.name wallet_name')
				->order('b.id desc')
				->paginate($limit);	
		$data = $data->toArray();
		if(empty($data['data'])){
			$data['data']  = Db::table('tp_balance_record')
				->alias('b')
				->join('tp_user u','u.id = b.uid','LEFT')
				->join('tp_coin_wallet w','w.id = b.wid','LEFT')
				->where('b.date','>=',1621094400)
				->where($where)
				->field('b.*,u.username,u.phone,w.name wallet_name')
				->order('b.id desc')
				->page(1)
				->limit($limit)
				->select();		
		}
		return $this->tableData($data);
	}
	/*
	资产统计
	 */
	public function total(){
		return $this->fetch();
	}
	/*
	资产统计
	 */
	public function totalList(){
		$limit = request()->param('limit')?:20;
		$data = Db::table('tp_coin_wallet')->where('status',1)->field('id,name')->paginate($limit);
		$data = $data->toArray();
		foreach ($data['data'] as $key => $val) {
			$data['data'][$key]['total_use'] = Db::table('tp_user_balance')->where('uid','in',$this->data_uids_arr)->where('wid',$val['id'])->sum('balance')?:0; 
		}
		return $this->tableData($data);
	}

	public function excel() {
		$limit      = request()->param('limit')?:20;
		$username   = request()->param('username');
		$phone   = request()->param('phone');
		$type   = request()->param('type');
		$remark   = request()->param('remark');
		$wallet_name  = request()->param('wallet_name');
		$uid   = request()->param('uid');
		$page      = request()->param('page');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');
		$where      = '1=1';
		if ($username != '') {
            $where .= ' and u.username like \'%' . $username . '%\'';
        }
        if ($remark != '') {
            $where .= ' and b.remark like \'%' . $remark . '%\'';
        }
        if ($phone != '') {
            $where .= ' and u.phone=' . $phone;
        }
        if($wallet_name != '') {
            $where .= ' and w.name like \'%'.$wallet_name . '%\'';
        }
        if($type != '') {
            $where .= ' and b.type>='.$type.' and b.type<'.($type+10);
        }
        if($uid != '') {
            $where .= ' and b.uid='.$uid;
        }
        if($start_time != '') {
            $where .= ' and b.date>="'.strtotime($start_time).'"';
        }
		if($end_time != '') {
            $where .= ' and b.date<="'.strtotime($end_time).'"';
        }
		$data  = Db::table('tp_balance_record')
				->alias('b')
				->join('tp_user u','u.id = b.uid','LEFT')
				->join('tp_coin_wallet w','w.id = b.wid','LEFT')
				->where('b.date','>=',1621094400)
				->where($where)
				->field('b.uid,u.username,u.phone,w.name wallet_name,b.before_balance,b.balance,b.after_balance,b.remark,from_unixtime(b.date) time')
				->order('b.id asc')
				->select();	
		return exportExcel(['用户编号', '用户名', '电话', '钱包', '变动前', '变动数量', '变动后', '变动原因', '时间'], $data);
	}
}